package com.woniu.service.impl;

import com.woniu.entity.Emp1;
import com.woniu.mapper.EmpDao;
import com.woniu.pojo.Emp;
import com.woniu.pojo.EmpExample;
import com.woniu.service.EmpService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpDao empDao;

    @Override
    public void add(Emp emp) {
        empDao.insertSelective(emp);
    }

    @Override
    public void addSelect(Emp emp) {
        empDao.insertSelective(emp);
    }

    @Override
    public void deleteById(Integer[] empno) {
        EmpExample empExample=new EmpExample();
        empExample.createCriteria().andEmpIdIn(Arrays.asList(empno));
        empDao.deleteByExample(empExample);
    }

    @Override
    public void deleteByExample(Integer[] empnos) {
        EmpExample empExample=new EmpExample();
        empExample.createCriteria().andEmpIdIn(Arrays.asList(empnos));
        empDao.deleteByExample(empExample);
    }


    @Override
    public void updateByExample(Emp emp) {
        EmpExample empExample=new EmpExample();

        empDao.updateByExampleSelective(emp,empExample);
    }

    @Override
    public void updateById(Emp emp) {
        empDao.updateByPrimaryKeySelective(emp);
    }

    @Override
    public List<Emp> selectAll() {
        return empDao.selectByExample(null);
    }

    @Override
    public List<Emp> selectExample(Emp emp) {
        EmpExample empExample=new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria();
        if (emp.getEmpName()!=null) {
            criteria.andEmpNameLike("%"+emp.getEmpName()+"%");
        }
        if (emp.getDeptId()!=null) {
            criteria.andDeptIdEqualTo(emp.getDeptId());
        }
        List<Emp> list = empDao.selectByExample(empExample);
        return list;
    }

    @Override
    public Emp1 findOne(Integer empno) {
        Emp1 emp = empDao.selectByPrimaryKey1(empno);
        return emp;
    }

    @Override
    public List<Emp1> findAll(Emp emp) {
        EmpExample empExample=new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria();
        if (emp.getEmpName()!=null) {
            criteria.andEmpNameLike("%"+emp.getEmpName()+"%");
        }
        if (emp.getDeptId()!=null) {
            criteria.andDeptIdEqualTo(emp.getDeptId());
        }
        List<Emp1> list = empDao.selectByExample1(empExample);
        return list;
    }

    @Override
    public void updateById1(Emp emp) {
        empDao.updateByPrimaryKeySelective(emp);
    }

    @Override
    public Integer countByExample() {
        long l = empDao.countByExample(null);
        return (int)l;
    }

}
